package top.mengchaob.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @title：
 * @author: mengchaob
 * @date: 2021年05月12日 14:36
 * @description:
 */
@Component
public class SpringCtx implements ApplicationContextAware {
    /**
     * 上下文对象实例
     */
    private static ApplicationContext applicationContext;

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringCtx.applicationContext = applicationContext;
    }

    public static <T> T getT(Class<T> class_){
        return SpringCtx.applicationContext.getBean(class_);
    }
    public static Object getTByName(String beanName){

        Object obj = null;
        try {
            obj = SpringCtx.applicationContext.getBean(beanName);
        }catch (RuntimeException run){
            run.printStackTrace();
        }
        return obj;
    }
}
